package com.qx.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qx.dao.UsersDao;
import com.qx.entity.Roles;
import com.qx.entity.Users;
import com.qx.entity.UsersSearch;
import com.qx.service.RolesService;
import com.qx.service.UsersService;
import com.qx.util.MD5Test2;
import com.qx.util.TokenUtil;



@RestController
public class UsersController {
@Autowired
private UsersService us;
@Autowired
private RolesService rs;
@Autowired
UsersDao ud;


@RequestMapping("/test")
public String test(){

	return "ok";
}
@RequestMapping("/login")
public Object login(String name, String password) {
	
	//加密密码
	// String ss = MD5Test2.generate(password);
	
	
	Users u=us.getUsers(name);//查询出来用户的数据
	Integer id=u.getId();//用户的id
	String zhuangtai=u.getIslockout();//查看用户的状态：是否锁定
	String b=new String("否");//做比对
	String mima=u.getPassword();//获取用户加密过的密码
	Boolean c=MD5Test2.verify(password,mima);//布尔值 为true，则有，反之亦然，比较密码相同否？
	if(c==false) {//如果用户5次都没有登陆成功，将会锁定账户
		int cc=us.updUsSuo(id);//记录没有登陆成功次数
		Users u1=us.getUsers(name);//再次查询数据
		int cc1=Integer.parseInt(u1.getPsdwrongtime());
		if(cc1>=3) {
			String z="是";
			System.out.println("cc1"+cc1);
			us.updZhangHu(z, id);
			
		}
		
	}
	String token = null;
	Map<String, Object> map = new HashMap<>();
	if(c==true && zhuangtai.equals(b)) {
		int a=us.updUsTime(u);//记录用户登陆时间
				try {
					token = TokenUtil.sign(u, 5 * 60 * 1000); // 1*60*60 * 1000 = 1小时有效期
					System.out.println("生成token大小=>" + token.length());
					System.out.println(token);
				} catch (UnsupportedEncodingException | JsonProcessingException e) {
					e.printStackTrace();
				}
				map.put("token", token);
				map.put("uname", u.getLoginname());
				map.put("uid", u.getId());
	        }else {
	        	String a="你的密码错误或账户锁定";
	        	map.put("cuo", a);
	        }
	        	  return map;
      }



//多条件分页查询
@RequestMapping("/findUsers")
public Object findUsers(UsersSearch u,Integer page,Integer limit) {
	Page<Users>list=us.find(u, page, limit);
	int count=us.count();
	Map<String, Object>map=new HashMap<String, Object>();
	map.put("code", 0);
	map.put("msg", "");
	map.put("count", count);
	map.put("data", list.getContent());
	return map;
}
//添加用户
@RequestMapping("/addUsers")
public Object addUsers(Users u) {
		return us.save(u);
}

//是否锁定账户
@RequestMapping("/zhuanghu")
public Object zhuanghu(String z, Integer id) {
	    us.updUsSuoTimes(id);//清空错误次数
		return us.updZhangHu(z, id);
}

//查询所有角色
@RequestMapping("/fujuese")
public Object fujuese() {
	System.out.println("查询角色controller");
	List<Roles>list=rs.getAll();
	return list;
}
//查询右侧所有角色
@RequestMapping("/userrole")
public List<Map<String, Object>> userrole(Integer userid) {
	List<Map<String, Object>> list=ud.getById(userid);
	
	for (Map<String, Object> map : list) {
		System.out.println(map);
	}
	System.out.println(list.get(0).get("1")+"1111111111111111111111");
	return list;
}

//给用户添加角色
@RequestMapping("/addUse")
public Integer addUse(Integer userid,Integer roleid) {
	int a=us.addUs(userid, roleid);
	return a;
}

//给用户删除角色
@RequestMapping("/delUse")
public Integer delUse(Integer userid,Integer roleid) {
	int b=us.delUs(userid, roleid);
	return b;
}

//重置用户密码
@RequestMapping("/updUsp")
public Integer updUsp(Users u) {
	int b=us.updUsp(u);
	return b;
}

}
